<template>
  <div class="pass">
    <!-- 通行证管理 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>住户管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <div class="sousuo">
        <div>
          <!-- 一个输入框，两个按钮 -->
          <el-button type="info" @click="getUserAdd">添加住户</el-button>
          <el-button type="info" @click="dorefresh">刷新</el-button>
        </div>

        <div class="search">
          <!-- 一个输入框，两个按钮 -->
          <el-input v-model="searchById" placeholder="请输入用户ID" />
          <el-button type="primary" @click="getUserById">搜索</el-button>
        </div>
      </div>
      <!-- 表格 -->
      <el-table :data="passList">
        <el-table-column prop="id" label="ID"></el-table-column>
        <el-table-column prop="uname" label="姓名"></el-table-column>
        <!-- 快速复制某行 alt+shift+剪头 -->
        <el-table-column prop="idCard" label="身份证号"></el-table-column>
        <el-table-column prop="gender" label="性别"></el-table-column>
        <el-table-column prop="age" label="年龄"></el-table-column>
        <el-table-column prop="address" label="住址"></el-table-column>
        <el-table-column prop="phone" label="手机号"></el-table-column>

        <el-table-column prop="istouch" label="有无接触史">
          <!-- 可以通过scope.row 拿到当前这一行数据 -->
          <template v-slot="scope">
            <!-- 0 待审核 1通过 2不通过 -->
            <el-tag v-if="scope.row.istouch == 1" type="warning"
              >有接触史</el-tag
            >
            <el-tag v-if="scope.row.istouch == 0" type="success"
              >无接触史</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column prop="note" label="备注"></el-table-column>

        <el-table-column label="当前状态">
          <!-- 可以通过scope.row 拿到当前这一行数据 -->
          <template v-slot="scope">
            <!-- 0 待审核 1通过 2不通过 -->
            <el-tag v-if="scope.row.ustatus == 0" type="warning">待审核</el-tag>
            <el-tag v-if="scope.row.ustatus == 1" type="success">通过</el-tag>
            <el-tag v-if="scope.row.ustatus == 2" type="danger">不通过</el-tag>
          </template>
        </el-table-column>
        <!-- 只有当状态为待审核，也就是0时，才展示两个按钮 -->
        <el-table-column label="审核" width="200px">
          <template v-slot="scope">
            <div v-if="scope.row.ustatus == 0">
              <el-button type="primary" @click="changeStatus(scope.row.id, 1)"
                >通过</el-button
              >
              <el-button type="danger" @click="changeStatus(scope.row.id, 2)"
                >不通过</el-button
              >
            </div>
          </template>
        </el-table-column>

        <!-- 只有当状态为待审核，也就是0时，才展示两个按钮 -->
        <el-table-column label="操作" width="200px">
          <template v-slot="scope">
            <div class="operation">
              <el-button
                color="green"
                @click="
                  gotoupdate(
                    scope.row.ustatus,
                    scope.row.id,
                    scope.row.uname,
                    scope.row.idCard,
                    scope.row.age,
                    scope.row.gender,
                    scope.row.address,
                    scope.row.phone,
                    scope.row.note
                  )
                "
                >编辑</el-button
              >
              <el-button color="red" @click="deleteBase(scope.row.id)"
                >删除</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="paging"
        background
        layout="prev, pager, next"
        :total="1000"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

  <script>
// getAllPassAPi获取所有通行证 发请求
import { ElMessage } from "element-plus";
import {
  getAllPassAPi,
  deletePassApi,
  changePassStatusApi,
  getUserPassApi,
} from "../api/index.js";
import {
  getAllBaseAPi,
  deleteBaseApi,
  changeUserStatusApi,
  getBaseByIdApi,
} from "../api/index1.js";
export default {
  inject: ["refresh"],
  data() {
    return {
      passList: [],
      searchById: "",
    };
  },
  mounted() {
    this.getAllBase();
  },
  methods: {
    getUserEdit() {
      this.$router.push({
        path: "../views/userEdit", //要跳转的页面的路由
      });
    },
    getUserAdd() {
      this.$router.push({
        path: "../views/userAdd", //要跳转的页面的路由
      });
    },
    // 1.获取住户列表的方法
    getAllBase() {
      getAllBaseAPi().then((res) => {
        console.log(res);
        this.passList = res.data;
        // console.log(this.passList)
      });
    },
    // 2.根据id删除用户
    deleteBase(id) {
      deleteBaseApi(id).then((res) => {
        console.log(res);
        // 1.刷新数据
        this.getAllBase();
        // 2.提示用户删除成功
        if (res.code == 20000) {
          ElMessage({
            message: "删除成功",
            type: "success",
          });
        }
      });
    },
    // 3.改变住户审核状态
    changeStatus(id, status) {
      console.log(id + "" + status);

      let obj = {
        id: id,
        ustatus: status,
      };
      changeUserStatusApi(obj).then((res) => {
        console.log(res);
        if (res.code == 20000) {
          this.getAllBase();
          ElMessage({
            message: "更新成功",
            type: "success",
          });
        }
      });
    },
    // 4.搜索
    getUserById() {
      getBaseByIdApi(this.searchById).then((res) => {
        console.log(res);
        this.passList = res.data;
      });
    },
    // 5.重置
    reset() {
      this.searchById = "";
      this.getAllBase();
    },
    // 6、go to update      time,title,message,level,num
    gotoupdate(ustatus, id, uname, idCard, age, gender, address, phone, note) {
      // 3.跳转，隐式传递参数 只能用name
      this.$router.push({
        // path:'../views/userEdit',
        name: "userEdit",
        params: {
          id: id,
          uname: uname,
          idCard: idCard,
          age: age,
          gender: gender,
          address: address,
          phone: phone,
          note: note,
          ustatus: ustatus,
        },
      });
    },
    //7.刷新的方法
    dorefresh() {
      this.refresh();
    },
  },
  created() {
    // this.getPassList();
  },
  add() {
    //通过push进行跳转
    this.$router.push("../views/userRegister.vue");
  },
};
</script>

  <style lang="less" scoped>
.pass {
  .el-breadcrumb {
    margin-bottom: 30px;
  }
}

.search {
  width: 300px;
  display: flex;

  .el-input {
    margin-right: 30px;
  }
}

.operation {
  display: flex;
}

.paging {
  display: flex;
  justify-content: right;
}

.sousuo {
  display: flex;
  justify-content: space-between;
}
</style>
